
// ExperimentSchedulerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ExperimentScheduler.h"
#include "ExperimentSchedulerDlg.h"
#include "DataSelectDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CExperimentSchedulerDlg dialog




CExperimentSchedulerDlg::CExperimentSchedulerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CExperimentSchedulerDlg::IDD, pParent)
	, m_strFolderPath(_T(""))
	, m_nMaxX(81)
	, m_strXAxis(_T("10,20,30,40,50,60,70,80"))
	, m_nMaxY(0)
	, m_strYAxis(_T(""))
	, m_strZAxis(_T(""))
	, m_nMaxZ(0)
	, m_nMinX(0)
	, m_nMinY(0)
	, m_nMinZ(0)
{

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CExperimentSchedulerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TAB1, m_cTabCtrl);
	DDX_Text(pDX, IDC_EDIT2, m_nMaxX);
	DDX_Text(pDX, IDC_EDIT3, m_strXAxis);
	DDX_Text(pDX, IDC_EDIT4, m_nMaxY);
	DDX_Text(pDX, IDC_EDIT5, m_strYAxis);
	DDX_Text(pDX, IDC_EDIT9, m_strZAxis);
	DDX_Text(pDX, IDC_EDIT8, m_nMaxZ);
	DDX_Text(pDX, IDC_EDIT6, m_nMinX);
	DDX_Text(pDX, IDC_EDIT7, m_nMinY);
	DDX_Text(pDX, IDC_EDIT10, m_nMinZ);
}

BEGIN_MESSAGE_MAP(CExperimentSchedulerDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_RADIO1, &CExperimentSchedulerDlg::OnBnClickedRadio1)
	ON_BN_CLICKED(IDC_RADIO2, &CExperimentSchedulerDlg::OnBnClickedRadio2)
	ON_BN_CLICKED(IDC_RADIO3, &CExperimentSchedulerDlg::OnBnClickedRadio3)
	ON_BN_CLICKED(IDC_RADIO4, &CExperimentSchedulerDlg::OnBnClickedRadio4)
	ON_BN_CLICKED(IDC_BUTTON_DRAW_NETWORK_SIZE, &CExperimentSchedulerDlg::OnBnClickedButtonDrawNetworkSize)
	ON_BN_CLICKED(IDC_BUTTON_DRAW_BUDGET, &CExperimentSchedulerDlg::OnBnClickedButtonDrawBudget)
	ON_BN_CLICKED(IDC_BUTTON_Draw_Data_Distribution, &CExperimentSchedulerDlg::OnBnClickedButtonDrawDataDistribution)
	ON_BN_CLICKED(IDC_BUTTON_Draw_Node_Search_Result, &CExperimentSchedulerDlg::OnBnClickedButtonDrawNodeSearchResult)
	ON_BN_CLICKED(IDC_BUTTON_Draw_Range_Search_Result, &CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchResult)
	ON_BN_CLICKED(IDC_BUTTON_DRAW_IN_DEGREE, &CExperimentSchedulerDlg::OnBnClickedButtonDrawInDegree)
	ON_BN_CLICKED(IDC_BUTTON_DRAW_Node_search_success_No, &CExperimentSchedulerDlg::OnBnClickedButtonDrawNodeSearchSuccessNo)
	
	ON_BN_CLICKED(IDC_BUTTON_CLEAR_X, &CExperimentSchedulerDlg::OnBnClickedButtonClearX)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR_Y, &CExperimentSchedulerDlg::OnBnClickedButtonClearY)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR_Z, &CExperimentSchedulerDlg::OnBnClickedButtonClearZ)
	ON_BN_CLICKED(IDC_BUTTON_Draw_Range_Search_Result2, &CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchResult2)
	ON_BN_CLICKED(IDC_BUTTON_Draw_Range_Search_NODE_VISIT_RATIO, &CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchNodeVisitRatio)
	ON_BN_CLICKED(IDC_BUTTON_Draw_Range_Search_Compare_Ratio, &CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchCompareRatio)
	ON_BN_CLICKED(IDC_BUTTON_Draw_Range_Search_NODE_VISIT_RATIO2, &CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchNodeVisitRatio2)
	ON_BN_CLICKED(IDC_BUTTON_Draw_Range_Search_Compare_Ratio2, &CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchCompareRatio2)
END_MESSAGE_MAP()


// CExperimentSchedulerDlg message handlers

BOOL CExperimentSchedulerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	this->m_cStaDlg.Create(IDD_DIALOG_STATIC_TEXT,&m_cTabCtrl);
	this->m_cDynDlg.Create(IDD_DIALOG_DYNAMIC_TEST,&m_cTabCtrl);
	this->m_cTabCtrl.AddTab(&m_cStaDlg,"Static Test",0);
	this->m_cTabCtrl.AddTab(&m_cDynDlg,"Dynamic Test",0);
	this->m_cTabCtrl.SelectTab(1);

	((CButton *)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);
	this->OnBnClickedRadio1();
	UpdataExperimentType();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CExperimentSchedulerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CExperimentSchedulerDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CExperimentSchedulerDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CExperimentSchedulerDlg::OnBnClickedRadio1()
{
	// TODO: Add your control notification handler code here
	this->m_nPlotType = MeanOnly;
}

void CExperimentSchedulerDlg::OnBnClickedRadio2()
{
	// TODO: Add your control notification handler code here
	m_nPlotType =  StdDevi;
}

void CExperimentSchedulerDlg::OnBnClickedRadio3()
{
	// TODO: Add your control notification handler code here
	m_nPlotType = MaxMin;
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawNetworkSize()
{
	// TODO: Add your control notification handler code here
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\network_size.log");
	CRawData cRawData;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nTime,  nSize;
		while(fin>>nTime>>nSize)
		{
			cRawData.Add(nTime,nSize,0,0,0);
		}
		this->Draw(cRawData,MeanOnly,"Network Size","Time(M)","Peer Number",m_strFolderPath+"Figure\\");
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawBudget()
{
	// TODO: Add your control notification handler code here
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\budget_log.log");
	CRawData cRawData;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nTime, nMax, nMin; double dMean,dSd;

		while(fin>>nTime>>dMean>>nMax>>nMin>>dSd)
		{
			cRawData.Add(nTime,dMean,nMin,nMax,dSd);
		}
		this->Draw(cRawData,this->m_nPlotType,"Bucket Distribution","Time(M)","Bucket Item Number",m_strFolderPath+"Figure\\");
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawDataDistribution()
{
	// TODO: Add your control notification handler code here
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\data_log.log");
	CRawData cRawData;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nTime, nMax, nMin; double dMean,dSd;

		while(fin>>nTime>>dMean>>nMax>>nMin>>dSd)
		{
			cRawData.Add(nTime,dMean,nMin,nMax,dSd);
		}
		this->Draw(cRawData,this->m_nPlotType,"Data Object Distribution","Time(M)","Local Data Number",m_strFolderPath+"Figure\\");
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawNodeSearchResult()
{
	// TODO: Add your control notification handler code here
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\node_Search.log");
	CRawData cRawData;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nTime, nNum,nMax, nMin; double dMean,dSd;

		while(fin>>nTime>>nNum>>dMean>>nMax>>nMin>>dSd)
		{
			cRawData.Add(nTime,dMean,nMin,nMax,dSd);
		}
		this->Draw(cRawData,this->m_nPlotType,"Node Query Result","Time(M)","Search Depth",m_strFolderPath+"Figure\\");
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchResult()
{
	// TODO: Add your control notification handler code here
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\data_search.log");
	map<int,CRawData> cRawDataMap;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nTime, nDepth;
		double dMax, dMin,dMean,dSd;

		while(fin>>nTime>>nDepth>>dMean>>dMax>>dMin>>dSd)
		{
			map<int,CRawData>::iterator it =cRawDataMap.find(nDepth);
			if(it==cRawDataMap.end())
			{
				it=cRawDataMap.insert(make_pair(nDepth,CRawData())).first;
			}
			it->second.Add(nTime,dMean,dMin,dMax,dSd);
		}
		if(this->m_nPlotType == MeanOnly)
		{
			this->Draw3D(cRawDataMap,"Range Query Result","Time(M)","Depth","Percentage(%)",m_strFolderPath+"Figure\\" );
		}
		else
		{
			vector<int> cTmp;
			for(map<int,CRawData>::iterator it =cRawDataMap.begin();it!=cRawDataMap.end();++it)
			{
				cTmp.push_back(it->first);
			}
			CDataSelectDialog cSelDiag(cTmp);
			if(cSelDiag.DoModal()==IDOK)
			{
				CString strName;
				strName.Format("Range Query Result (Depth: %d)",cSelDiag.GetSelData());
				this->Draw(cRawDataMap[cSelDiag.GetSelData()],m_nPlotType,strName,"Time(M)","Percentage(%)",m_strFolderPath+"Figure\\" );
			}
		}
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}


int CExperimentSchedulerDlg::Draw(const CRawData& cRawData, const PLOT_TYPE nType, const CString & strName, const CString & strX,const CString & strY,const CString & strOutputPath)
{
	this->UpdateData();
	CString strScriptNamePath(strOutputPath);
	if(strScriptNamePath[strScriptNamePath.GetLength()-1]!='\\')
	{
		strScriptNamePath.AppendChar('\\');
	}
	CString strRawDataNamePath(strScriptNamePath);
	CString strFigureNamePath(strScriptNamePath);
	
	CString strFileName;
	for(int i = 0;i<strName.GetLength();++i)
	{
		char c = strName[i];
		if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))
		{
			strFileName.AppendChar(c);
		}
	}
	switch(nType)
	{
		
		case StdDevi:
		{
			strScriptNamePath.AppendFormat("%s_%s.plt",strFileName.GetString(),"SD");
			strRawDataNamePath.AppendFormat("%s_%s.dat",strFileName.GetString(),"SD");
			strFigureNamePath.AppendFormat("%s_%s.eps",strFileName.GetString(),"SD");
		}
		break;
		case MaxMin:
		{
			strScriptNamePath.AppendFormat("%s_%s.plt",strFileName.GetString(),"MaxMin");
			strRawDataNamePath.AppendFormat("%s_%s.dat",strFileName.GetString(),"MaxMin");
			strFigureNamePath.AppendFormat("%s_%s.eps",strFileName.GetString(),"MaxMin");
		}
		break;
		case Both:
		{
			strScriptNamePath.AppendFormat("%s_%s.plt",strFileName.GetString(),"Both");
			strRawDataNamePath.AppendFormat("%s_%s.dat",strFileName.GetString(),"Both");
			strFigureNamePath.AppendFormat("%s_%s.eps",strFileName.GetString(),"Both");
		}
		break;
		default:
		{
			strScriptNamePath.AppendFormat("%s_%s.plt",strFileName.GetString(),"Mean");
			strRawDataNamePath.AppendFormat("%s_%s.dat",strFileName.GetString(),"Mean");
			strFigureNamePath.AppendFormat("%s_%s.eps",strFileName.GetString(),"Mean");
		}
		break;
	}
	ofstream fout(strRawDataNamePath.GetString());
	if(fout)
	{
		for (unsigned i = 0;i<cRawData.size();i++)
		{
			fout<<cRawData.ToString(i).GetString()<<endl;	
		}
		fout.close();
	}
	else
	{
		this->MessageBox("Create Data File Error!");
		return 1;
	}
	CString script("reset \n");
	if(nType==Both)
	{
		script.AppendFormat("set term postscript eps color enhanced \n");
		script.AppendFormat("set output '%s'\n",strFigureNamePath.GetString());
		script.AppendFormat("unset key \n");
		script.AppendFormat("set multiplot\n");
		script.AppendFormat("set size 0.4,0.6\n");
		script.AppendFormat("set origin 0.1,0.1\n");
		script.AppendFormat("set title '(a) SD error bar'\n");
		if(this->m_nMaxX!=0)
		{
			script.AppendFormat("set xrange [%d:%d] \n",m_nMinX,m_nMaxX);
		}
		if(m_strXAxis.GetLength()>1)
		{
			script.AppendFormat("set xtics (%s) \n",m_strXAxis.GetString());
		}
		if(m_nMaxY!=0)
		{
			script.AppendFormat("set yrange [%d:%d] \n",m_nMinY,m_nMaxY);
		}	
		if(m_strYAxis.GetLength()>1)
		{
			script.AppendFormat("set ytics (%s) \n",m_strYAxis.GetString());
		}
		script.AppendFormat("set xlabel '%s' \n",strX.GetString());
		script.AppendFormat("set ylabel '%s'\n",strY.GetString());
		script.AppendFormat("plot '%s' using 1:2:5:6   with yerrorbars lt 1 lw 1 ,",strRawDataNamePath.GetString());
		script.AppendFormat("'%s' using 1:2   with line lt 1 lw 2 \n",strRawDataNamePath.GetString());
				
		script.AppendFormat("set size 0.4,0.6\n");
		script.AppendFormat("set origin 0.5,0.1\n");
		script.AppendFormat("set title '(b) Max-Min error bar'\n");
		if(this->m_nMaxX!=0)
		{
			script.AppendFormat("set xrange [%d:%d] \n",m_nMinX,m_nMaxX);
		}
		if(m_strXAxis.GetLength()>1)
		{
			script.AppendFormat("set xtics (%s) \n",m_strXAxis.GetString());
		}
		if(m_nMaxY!=0)
		{
			script.AppendFormat("set yrange [%d:%d] \n",m_nMinY,m_nMaxY);
		}	
		if(m_strYAxis.GetLength()>1)
		{
			script.AppendFormat("set ytics (%s) \n",m_strYAxis.GetString());
		}
		script.AppendFormat("set xlabel '%s' \n",strX.GetString());
		script.AppendFormat("set ylabel '%s'\n",strY.GetString());
		script.AppendFormat("plot '%s' using 1:2:3:4   with yerrorbars lt 1 lw 1 ,",strRawDataNamePath.GetString());
		script.AppendFormat("'%s' using 1:2   with line lt 1 lw 2 \n",strRawDataNamePath.GetString());
				
		script.AppendFormat("unset multiplot\n");


		script.AppendFormat("reset \n");
		script.AppendFormat("set term windows\n");

		script.AppendFormat("unset key \n");
		script.AppendFormat("set multiplot\n");
		script.AppendFormat("set size 0.4,0.6\n");
		script.AppendFormat("set origin 0.1,0.1\n");
		script.AppendFormat("set title '(a) SD error bar'\n");
		if(this->m_nMaxX!=0)
		{
			script.AppendFormat("set xrange [%d:%d] \n",m_nMinX,m_nMaxX);
		}
		if(m_strXAxis.GetLength()>1)
		{
			script.AppendFormat("set xtics (%s) \n",m_strXAxis.GetString());
		}
		if(m_nMaxY!=0)
		{
			script.AppendFormat("set yrange [%d:%d] \n",m_nMinY,m_nMaxY);
		}	
		if(m_strYAxis.GetLength()>1)
		{
			script.AppendFormat("set ytics (%s) \n",m_strYAxis.GetString());
		}
		script.AppendFormat("set xlabel '%s' \n",strX.GetString());
		script.AppendFormat("set ylabel '%s'\n",strY.GetString());
		script.AppendFormat("plot '%s' using 1:2:5:6   with yerrorbars lt 1 lw 1 ,",strRawDataNamePath.GetString());
		script.AppendFormat("'%s' using 1:2   with line lt 1 lw 2 \n",strRawDataNamePath.GetString());
				
		script.AppendFormat("set size 0.4,0.6\n");
		script.AppendFormat("set origin 0.5,0.1\n");
		script.AppendFormat("set title '(b) Max-Min error bar'\n");
		if(this->m_nMaxX!=0)
		{
			script.AppendFormat("set xrange [%d:%d] \n",m_nMinX,m_nMaxX);
		}
		if(m_strXAxis.GetLength()>1)
		{
			script.AppendFormat("set xtics (%s) \n",m_strXAxis.GetString());
		}
		if(m_nMaxY!=0)
		{
			script.AppendFormat("set yrange [%d:%d] \n",m_nMinY,m_nMaxY);
		}	
		if(m_strYAxis.GetLength()>1)
		{
			script.AppendFormat("set ytics (%s) \n",m_strYAxis.GetString());
		}
		script.AppendFormat("set xlabel '%s' \n",strX.GetString());
		script.AppendFormat("set ylabel '%s'\n",strY.GetString());
		script.AppendFormat("plot '%s' using 1:2:3:4   with yerrorbars lt 1 lw 1 ,",strRawDataNamePath.GetString());
		script.AppendFormat("'%s' using 1:2   with line lt 1 lw 2 \n",strRawDataNamePath.GetString());
		
		script.AppendFormat("unset multiplot\n");
		script.AppendFormat("reset \n");
	}
	else
	{
		script.AppendFormat("set term postscript eps color enhanced \n");
		script.AppendFormat("set title '%s'\n",strName.GetString());
		script.AppendFormat("set output '%s'\n",strFigureNamePath.GetString());
		script.AppendFormat("unset key \n");
		if(this->m_nMaxX!=0)
		{
			script.AppendFormat("set xrange [%d:%d] \n",m_nMinX,m_nMaxX);
		}
		if(m_strXAxis.GetLength()>1)
		{
			script.AppendFormat("set xtics (%s) \n",m_strXAxis.GetString());
		}
		if(m_nMaxY!=0)
		{
			script.AppendFormat("set yrange [%d:%d] \n",m_nMinY,m_nMaxY);
		}	
		if(m_strYAxis.GetLength()>1)
		{
			script.AppendFormat("set ytics (%s) \n",m_strYAxis.GetString());
		}	
		script.AppendFormat("set xlabel '%s' \n",strX.GetString());
		script.AppendFormat("set ylabel '%s'\n",strY.GetString());
		switch(nType)
		{
			case StdDevi:
			{
				script.AppendFormat("plot '%s' using 1:2:5:6   with yerrorbars lt 1 lw 1 ,",strRawDataNamePath.GetString());
				script.AppendFormat("'%s' using 1:2   with line lt 1 lw 2 \n",strRawDataNamePath.GetString());
				break;
			}
			case MaxMin:
			{
				script.AppendFormat("plot '%s' using 1:2:3:4   with yerrorbars lt 1 lw 1 ,",strRawDataNamePath.GetString());
				script.AppendFormat("'%s' using 1:2   with line lt 1 lw 2 \n",strRawDataNamePath.GetString());
				break;
			}
			default:
			{
				script.AppendFormat("plot '%s' using 1:2   with linespoints lt 1 lw 2 \n",strRawDataNamePath.GetString());
				break;
			}
		}
		script.AppendFormat("set term windows\n");
		script.AppendFormat("replot\n");
	}
	FILE *pFile ;
	if(0==fopen_s(&pFile,strScriptNamePath.GetString(),"w"))
	{
		fwrite(script.GetBuffer(),1,script.GetLength(),pFile);
		fclose(pFile);
		CString cmd;
		cmd.Format("pgnuplot.exe -persist \"%s\"",strScriptNamePath.GetString());
		WinExec(cmd.GetString(),SW_SHOW);
	}
	else
	{
		MessageBox("Open File Error!");
		return 2;
	}
	return 0;
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawInDegree()
{
	// TODO: Add your control notification handler code here
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\incoming_link.log");
	CRawData cRawData;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nTime, nMax, nMin; double dMean,dSd;

		while(fin>>nTime>>dMean>>nMax>>nMin>>dSd)
		{
			cRawData.Add(nTime,dMean,nMin,nMax,dSd);
		}
		this->Draw(cRawData,this->m_nPlotType,"Incoming Link Distribution","Time(M)","Incoming Link Number",m_strFolderPath+"Figure\\");
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}

void CExperimentSchedulerDlg::UpdataExperimentType(void)
{
	this->UpdateData();
	m_strFolderPath.GetBufferSetLength(MAX_PATH+1);
	GetModuleFileName(NULL, m_strFolderPath.GetBuffer(), MAX_PATH);
	m_strFolderPath.Delete(m_strFolderPath.ReverseFind('\\')+1,m_strFolderPath.GetLength()-1-m_strFolderPath.ReverseFind('\\'));
			
	switch(this->m_cTabCtrl.GetCurSel())
	{
	case 0:
		{
			m_strFolderPath.Append("StaticTest\\");
			break;
		}
	case 1:
		{
			m_strFolderPath.Append("DynamicTest\\");
			break;
		}
	}
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawNodeSearchSuccessNo()
{
	// TODO: Add your control notification handler code here
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\node_Search.log");
	CRawData cRawData;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nTime, nNum,nMax, nMin; double dMean,dSd;

		while(fin>>nTime>>nNum>>dMean>>nMax>>nMin>>dSd)
		{
			cRawData.Add(nTime,nNum,0,0,0);
		}
		this->Draw(cRawData,MeanOnly,"Node Search Success Number","Time(M)","Number of Accessed Node",m_strFolderPath+"Figure\\" );
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}

void CExperimentSchedulerDlg::Draw3D(map<int,CRawData> & cData, const CString & strTitle,const CString & strX,const CString & strY,const CString & strZ, const CString & strOutputPath)
{
	CString strScriptNamePath(strOutputPath);
	this->UpdateData();
	if(strScriptNamePath[strScriptNamePath.GetLength()-1]!='\\')
	{
		strScriptNamePath.AppendChar('\\');
	}
	CString strRawDataNamePath(strScriptNamePath);
	CString strFigureNamePath(strScriptNamePath);
	CString strFileName;
	for(int i = 0;i<strTitle.GetLength();++i)
	{
		char c = strTitle[i];
		if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))
		{
			strFileName.AppendChar(c);
		}
	}
	strScriptNamePath.AppendFormat("%s_%s.plt",strFileName.GetString(),"3D");
	strRawDataNamePath.AppendFormat("%s_%s.dat",strFileName.GetString(),"3D");
	strFigureNamePath.AppendFormat("%s_%s.eps",strFileName.GetString(),"3D");
	ofstream fout(strRawDataNamePath.GetString());
	if(fout)
	{
		unsigned nSize = cData.begin()->second.size();
		for(map<int,CRawData>::iterator it = cData.begin();it!=cData.end();++it)
		{
			nSize = min(nSize,it->second.size());
		}
		for(unsigned i = 0;i<nSize;i++)
		{
			for(map<int,CRawData>::iterator it = cData.begin();it!=cData.end();++it)
			{
				fout<<it->second[i][0]/60000<<" "<<D2I(it->second[i][1])<<" "<<it->first<<" ";
			}
			fout<<endl;
		}
		fout.close();
	}
	else
	{
		this->MessageBox("Create Data File Error!");
		return ;
	}
	CString script("reset \n");
	script.AppendFormat("set term postscript eps color enhanced \n");
	script.AppendFormat("set title '%s'\n",strTitle.GetString());
	script.AppendFormat("set output '%s'\n",strFigureNamePath.GetString());
	script.AppendFormat("unset key \nset ticslevel 0\n");
	script.AppendFormat("set xlabel '%s' \n",strX.GetString());
	script.AppendFormat("set ylabel '%s'\n",strY.GetString());
	script.AppendFormat("set zlabel '%s'\n",strZ.GetString());
	if(this->m_nMaxX!=0)
	{
		script.AppendFormat("set xrange [%d:%d] \n",m_nMinX,m_nMaxX);
	}
	if(m_strXAxis.GetLength()>1)
	{
		script.AppendFormat("set xtics (%s) \n",m_strXAxis.GetString());
	}
	if(m_nMaxY!=0)
	{
		script.AppendFormat("set yrange [%d:%d] \n",m_nMinY,m_nMaxY);
	}	
	if(m_strYAxis.GetLength()>1)
	{
		script.AppendFormat("set ytics (%s) \n",m_strYAxis.GetString());
	}
	if(m_nMaxZ!=0)
	{
		script.AppendFormat("set zrange [%d:%d] \n",m_nMinZ,m_nMaxZ);
	}	
	if(m_strZAxis.GetLength()>1)
	{
		script.AppendFormat("set ztics (%s) \n",m_strZAxis.GetString());
	}
	script.AppendFormat("set grid\n");
	script.AppendFormat("set view 38,315\n");
	script.Append("splot ");
	for(unsigned i = 0;i<cData.size();i++)
	{
		script.AppendFormat("'%s' using %u:%u:%u with linespoints  pointtype %u lt %u lw 2 , '%s' using %u:%u:%u with impulses  lt %u lw 1"
			,strRawDataNamePath.GetString(),i*3+1,i*3+3,i*3+2,i+1,i+1,strRawDataNamePath.GetString(),i*3+1,i*3+3,i*3+2,i+1);
	
		if(i!=cData.size()-1)
		{
			script.AppendChar(',');
		}
		else
		{
			script.AppendChar('\n');
		}
	}
	script.AppendFormat("set term windows\n");
	script.AppendFormat("replot\n");
	FILE *pFile ;
	if(0==fopen_s(&pFile,strScriptNamePath.GetString(),"w"))
	{
		fwrite(script.GetBuffer(),1,script.GetLength(),pFile);
		fclose(pFile);
		CString cmd;
		cmd.Format("pgnuplot.exe -persist \"%s\"",strScriptNamePath.GetString());
		WinExec(cmd.GetString(),SW_SHOW);
	}
	else
	{
		MessageBox("Open File Error!");
	}
	return;
}

void CExperimentSchedulerDlg::OnBnClickedRadio4()
{
	// TODO: Add your control notification handler code here
	m_nPlotType = Both;
}

void CExperimentSchedulerDlg::OnBnClickedButtonClearX()
{
	// TODO: Add your control notification handler code here
	m_nMinX = 0;
	m_nMaxX = 0;
	m_strXAxis.Empty();
	this->UpdateData(FALSE);
}

void CExperimentSchedulerDlg::OnBnClickedButtonClearY()
{
	// TODO: Add your control notification handler code here
	m_nMinY = 0;
	m_nMaxY = 0;
	m_strYAxis.Empty();
	this->UpdateData(FALSE);
}

void CExperimentSchedulerDlg::OnBnClickedButtonClearZ()
{
	// TODO: Add your control notification handler code here
	m_nMinZ = 0;
	m_nMaxZ = 0;
	m_strZAxis.Empty();
	this->UpdateData(FALSE);
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchResult2()
{
	// TODO: Add your control notification handler code here
		// TODO: Add your control notification handler code here
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\Dyanmic_range_test_log.log");
	map<int,CRawData> cRawDataMap;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nRadius, nDepth;double dMean,dSd,dMax, dMin ;

		while(fin>>nRadius>>nDepth>>dMean>>dMax>>dMin>>dSd)
		{
			map<int,CRawData>::iterator it =cRawDataMap.find(nDepth);
			if(it==cRawDataMap.end())
			{
				it=cRawDataMap.insert(make_pair(nDepth,CRawData())).first;
			}
			it->second.Add(nRadius*60000,dMean,dMin,dMax,dSd);
		}
		if(this->m_nPlotType == MeanOnly)
		{
			this->Draw3D(cRawDataMap,"Range Query Result with Different Radius","Search Radius","Depth","Percentage(%)",m_strFolderPath+"Figure\\" );
		}
		else
		{
			vector<int> cTmp;
			for(map<int,CRawData>::iterator it =cRawDataMap.begin();it!=cRawDataMap.end();++it)
			{
				cTmp.push_back(it->first);
			}
			CDataSelectDialog cSelDiag(cTmp);
			if(cSelDiag.DoModal()==IDOK)
			{
				CString strName;
				strName.Format("Range Query Result with Different Radius(Depth: %d)",cSelDiag.GetSelData());
				this->Draw(cRawDataMap[cSelDiag.GetSelData()],m_nPlotType,strName,"Search Radius","Percentage(%)",m_strFolderPath+"Figure\\" );
			}
		}
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchNodeVisitRatio()
{
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\visit_ratio_log.log");
	ifstream fin(strData.GetString());
	map<int,CRawData> cRawDataMap;
	if(fin)
	{
		int nTime, nDepth;
		double dMax, dMin,dMean,dSd;

		while(fin>>nTime>>nDepth>>dMean>>dMax>>dMin>>dSd)
		{
			map<int,CRawData>::iterator it =cRawDataMap.find(nDepth);
			if(it==cRawDataMap.end())
			{
				it=cRawDataMap.insert(make_pair(nDepth,CRawData())).first;
			}
			it->second.Add(nTime,dMean,dMin,dMax,dSd);
		}
		if(this->m_nPlotType == MeanOnly)
		{
			this->Draw3D(cRawDataMap,"Peer Visited Ratio","Time(M)","Depth","Percentage(%)",m_strFolderPath+"Figure\\" );
		}
		else
		{
			vector<int> cTmp;
			for(map<int,CRawData>::iterator it =cRawDataMap.begin();it!=cRawDataMap.end();++it)
			{
				cTmp.push_back(it->first);
			}
			CDataSelectDialog cSelDiag(cTmp);
			if(cSelDiag.DoModal()==IDOK)
			{
				CString strName;
				strName.Format("Peer Visited Ratio (Depth: %d)",cSelDiag.GetSelData());
				this->Draw(cRawDataMap[cSelDiag.GetSelData()],m_nPlotType,strName,"Time(M)","Percentage(%)",m_strFolderPath+"Figure\\" );
			}
		}
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchCompareRatio()
{
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\data_ratio_log.log");
	ifstream fin(strData.GetString());
	map<int,CRawData> cRawDataMap;
	if(fin)
	{
		int nTime, nDepth;
		double dMax, dMin,dMean,dSd;

		while(fin>>nTime>>nDepth>>dMean>>dMax>>dMin>>dSd)
		{
			map<int,CRawData>::iterator it =cRawDataMap.find(nDepth);
			if(it==cRawDataMap.end())
			{
				it=cRawDataMap.insert(make_pair(nDepth,CRawData())).first;
			}
			it->second.Add(nTime,dMean,dMin,dMax,dSd);
		}
		if(this->m_nPlotType == MeanOnly)
		{
			this->Draw3D(cRawDataMap,"Data Involved Ratio","Time(M)","Depth","Percentage(%)",m_strFolderPath+"Figure\\" );
		}
		else
		{
			vector<int> cTmp;
			for(map<int,CRawData>::iterator it =cRawDataMap.begin();it!=cRawDataMap.end();++it)
			{
				cTmp.push_back(it->first);
			}
			CDataSelectDialog cSelDiag(cTmp);
			if(cSelDiag.DoModal()==IDOK)
			{
				CString strName;
				strName.Format("Data Involved Ratio (Depth: %d)",cSelDiag.GetSelData());
				this->Draw(cRawDataMap[cSelDiag.GetSelData()],m_nPlotType,strName,"Time(M)","Percentage(%)",m_strFolderPath+"Figure\\" );
			}
		}
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}


void CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchNodeVisitRatio2()
{
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\visit_ratio_drange_log.log");
	map<int,CRawData> cRawDataMap;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nRadius, nDepth;double dMean,dSd,dMax, dMin ;

		while(fin>>nRadius>>nDepth>>dMean>>dMax>>dMin>>dSd)
		{
			map<int,CRawData>::iterator it =cRawDataMap.find(nDepth);
			if(it==cRawDataMap.end())
			{
				it=cRawDataMap.insert(make_pair(nDepth,CRawData())).first;
			}
			it->second.Add(nRadius*60000,dMean,dMin,dMax,dSd);
		}
		if(this->m_nPlotType == MeanOnly)
		{
			this->Draw3D(cRawDataMap,"Peer Visited Ratio with Different Radius","Search Radius","Depth","Percentage(%)",m_strFolderPath+"Figure\\" );
		}
		else
		{
			vector<int> cTmp;
			for(map<int,CRawData>::iterator it =cRawDataMap.begin();it!=cRawDataMap.end();++it)
			{
				cTmp.push_back(it->first);
			}
			CDataSelectDialog cSelDiag(cTmp);
			if(cSelDiag.DoModal()==IDOK)
			{
				CString strName;
				strName.Format("Peer Visited Ratio with Different Radius(Depth: %d)",cSelDiag.GetSelData());
				this->Draw(cRawDataMap[cSelDiag.GetSelData()],m_nPlotType,strName,"Search Radius","Percentage(%)",m_strFolderPath+"Figure\\" );
			}
		}
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}
}

void CExperimentSchedulerDlg::OnBnClickedButtonDrawRangeSearchCompareRatio2()
{
	this->UpdataExperimentType();
	CString strData(m_strFolderPath);
	strData.Append("result\\data_ratio_drange_log.log");
	map<int,CRawData> cRawDataMap;
	ifstream fin(strData.GetString());
	if(fin)
	{
		int nRadius, nDepth;double dMean,dSd,dMax, dMin ;

		while(fin>>nRadius>>nDepth>>dMean>>dMax>>dMin>>dSd)
		{
			map<int,CRawData>::iterator it =cRawDataMap.find(nDepth);
			if(it==cRawDataMap.end())
			{
				it=cRawDataMap.insert(make_pair(nDepth,CRawData())).first;
			}
			it->second.Add(nRadius*60000,dMean,dMin,dMax,dSd);
		}
		if(this->m_nPlotType == MeanOnly)
		{
			this->Draw3D(cRawDataMap,"Data Involved Ratio with Different Radius","Search Radius","Depth","Percentage(%)",m_strFolderPath+"Figure\\" );
		}
		else
		{
			vector<int> cTmp;
			for(map<int,CRawData>::iterator it =cRawDataMap.begin();it!=cRawDataMap.end();++it)
			{
				cTmp.push_back(it->first);
			}
			CDataSelectDialog cSelDiag(cTmp);
			if(cSelDiag.DoModal()==IDOK)
			{
				CString strName;
				strName.Format("Data Involved Ratio with Different Radius(Depth: %d)",cSelDiag.GetSelData());
				this->Draw(cRawDataMap[cSelDiag.GetSelData()],m_nPlotType,strName,"Search Radius","Percentage(%)",m_strFolderPath+"Figure\\" );
			}
		}
	}
	else
	{
		MessageBox("Can not Open Raw data file","Error",MB_OK|MB_ICONERROR);
	}// TODO: Add your control notification handler code here
}
